package org.esk.dablog.web.forms.forum;

import org.esk.dablog.web.forms.AddEntryForm;
import org.esk.dablog.web.util.RequestUtils;
import org.esk.dablog.model.Entry;
import org.esk.dablog.model.forum.ForumTopicEntry;
import org.esk.dablog.model.forum.ForumPostEntry;
import org.esk.dablog.exceptions.BusinessException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;
import org.springframework.validation.BindException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * This class implements "add topic" task
 * User: esk
 * Date: 08.12.2006
 * Time: 20:26:31
 * $Id:$
 */
public class AddPostForm extends AddEntryForm {


    protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse httpServletResponse, Object object, BindException bindException) throws Exception {
        Entry e = (Entry) object;
        super.onSubmit(request, httpServletResponse, object, bindException);
        return new ModelAndView(new RedirectView(getSuccessView() + "/" + e.getId()));
    }

    protected void processEntry(HttpServletRequest request, Entry entry) throws Exception {
    }

    protected Object formBackingObject(HttpServletRequest httpServletRequest) throws Exception {
        long id = RequestUtils.extractLongParameter(httpServletRequest);
        ForumTopicEntry topic = (ForumTopicEntry) getEntryManager().getEntry(getCommandClass(), id);
        ForumPostEntry post= new ForumPostEntry();
        post.setParent(topic);
        return post;
    }
}
